/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.program;


import java.util.Collection;

import dex.compiler.model.base.Place;
import dex.compiler.model.definition.Container;
import dex.compiler.model.definition.Definition;
import dex.compiler.model.definition.Unit;
import dex.misc.Conversion;
import dex.misc.Converter;


/**
 * A path to a definition.
 */
public class Path {


	/**
	 * The program that owns the definition.
	 */
	final private Program program;
	
	
	/**
	 * The unit that owns the definition.
	 */
	final private Unit unit;

	
	/**
	 * The container that owns the definition.
	 * May be the same as unit if it's a toplevel definition.
	 */
	final private Container container;


	/**
	 * The definition.
	 */
	final private Definition definition;
	
	
	/**
	 * Constructs a new path to a toplevel definition.
	 * 
	 * @param program  the program that owns the definition
	 * @param unit     the unit that owns the definition
	 * @param container  the container that owns the definition
	 * @param definition  the definition
	 */
	public Path(Program program, Unit unit, Container container, Definition definition) {
		super();
		this.program = program;
		this.unit = unit;
		this.container = container;
		this.definition = definition;
	}


	/**
	 * Returns the definition.
	 * 
	 * @return  the definition
	 */
	public Definition getDefinition() {
		return definition;
	}


	/**
	 * Returns the program that owns the definition.
	 * 
	 * @return  the program that owns the definition
	 */
	public Program getProgram() {
		return program;
	}


	/**
	 * Returns the unit that owns the definition.
	 * 
	 * @return  the unit that owns the definition
	 */
	public Unit getUnit() {
		return unit;
	}
	
	
	/**
	 * Returns the container that owns the definition.
	 * 
	 * @return  the container that owns the definition
	 */
	public Container getContainer() {
		return container;
	}


	/**
	 * Returns the fully qualified name of the definition.
	 * 
	 * @return  returns the fully qualified name of the definition
	 */
	public String qualified() {
		return unit.getName() + ":" + definition.getName();
	}


	/**
	 * Returns the places of the definitions in the given paths.
	 * The returned collection is unmodifiable.
	 * 
	 * @param paths  the paths whose places to return
	 * @return       the places of those paths
	 */
	public static Collection<Place> places(Collection<Path> paths) {
		return new Conversion<Path,Place>(paths, new Converter<Path,Place>() {
			public Place convert(Path path) {
				return path.getDefinition().getPlace();
			}
		});
	}

}
